﻿using System;
using System.Collections;

public class ViewModelHelper
{
	public ViewModelHelper(){ }

    public AlbumViewModel[] SortArray(AlbumViewModel[]array, string sortEpression,string sortDirection)
    {
        AlbumViewModel min;
        AlbumViewModel r;
        int h;

        for (int i = 0; i < array.Length; i++)
        {
            min = array[i];
            h = i;
            for (int j = i+1; j < array.Length; j++)
            {
                int result=0;
                switch (sortEpression)
                {
                    case "name": result = String.Compare(array[j].Name, min.Name);
                        break;
                    case "author": result = String.Compare(array[j].Author, min.Author);
                        break;
                    case "date": result = DateTime.Compare(array[j].Date, min.Date);
                        break;
                    default: break;

                }
               
                if (result==-1)
                {
                    min = array[j];
                    h = j;
                }
            }
            r = array[i];
            array[i] = array[h];
            array[h] = r;
        }

        if (sortDirection == "ASC") { return array; }
        else { return ReverseArray(array); }
       
    }

    public AlbumViewModel[] ReverseArray(AlbumViewModel[] array)
    {
        Array.Reverse(array);
        return array;
    }

    public AlbumViewModel[] GetFiltratedModels(AlbumViewModel[]array, string query)
    {
        ArrayList filtratedArray = new ArrayList();

        foreach (AlbumViewModel model in array)
        {
            if (model.Name.ToLower() == query.ToLower() || model.Date.ToString("dd.MM.yyyy") == query || model.Author.ToLower() == query.ToLower())
            {
                if (!filtratedArray.Contains(model))
                {
                    filtratedArray.Add(model);
                }
            }
        }

        AlbumViewModel[] result = new AlbumViewModel[filtratedArray.Count];
        int i = 0;
        foreach (AlbumViewModel model in filtratedArray)
        {
            result[i] = model;
            i++;
        }

        return result;
    }
}